package com.kaikeba.wx.controller;

import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.ExpressService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.JsonUtil;
import com.kaikeba.util.RandomUtil;
import com.kaikeba.util.SMSUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-08-03 14:09
 * @Modified By:
 */
public class UserController {

    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest req,HttpServletResponse resp){
        String userphone = req.getParameter("userphone");
        String code = RandomUtil.getCode()+"";
        System.out.println("短信验证码为："+code);
        //boolean flag = SMSUtil.loginSMS(userphone, code);
        Message message = new Message();
        message.setStatus(0);
        message.setResult("验证码已经发送，请查收！");
        /*if (flag){
            //短信发送成功！
            message.setStatus(0);
            message.setResult("验证码已经发送，请查收！");
        }else{
            //短信发送失败！
            message.setStatus(1);
            message.setResult("验证码发送失败，请检查手机号或稍后再试！");
        }*/
        UserUtil.setLoginSms(req.getSession(),userphone,code);
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        String userphone = req.getParameter("userphone");
        String userCode = req.getParameter("code");
        String sysCode = UserUtil.getLoginSms(req.getSession(),userphone);
        Message msg = new Message();
        if (sysCode==null){
            //表示 未发送短信
            msg.setStatus(-1);
            msg.setResult("未发送短信！");
        }else if (Objects.equals(userCode,sysCode)){

            /*
            * 思路：先根据电话号码到数据库中快递员表中进行查找
            *           1.到快递员表中 找到： 则更新数据库的最近登陆时间，并返回快递员的信息
            *               2.在快递员表中未找到，则到用户表中去找，
            *                           用户表中找到，返回用户信息
            *                           未找到：则插入新的用户信息，并返回用户信息
            * */
            Courier courier = CourierService.findByphonenumber(userphone);
            if (courier!=null){
                //验证成功！进行登录
                msg.setStatus(1);
                //在快递员表中找到，更新最近登录的时间信息
                CourierService.updatetime(userphone);
                Courier courier1 = CourierService.findByphonenumber(userphone);
                UserUtil.setWxCourier(req.getSession(),courier1);
            }else{
                //在快递员表中未找到
                //验证成功！进行登录
                msg.setStatus(0);
                //1.进行查找
                User user = UserService.findByphonenumber(userphone);
                if (user!=null){
                    //找到，则更新数据库信息
                    UserService.updatetime(userphone);
                    //返回user对象
                    User newUser = UserService.findByphonenumber(userphone);
                    UserUtil.setWxUser(req.getSession(),newUser);
                }else{
                    //未找到,新增用户信息
                    User user1 = new User(1,"张三",userphone,"123456","1234567",null,null);
                    UserService.insert(user1);
                    UserUtil.setWxUser(req.getSession(),user1);
                }
            }
        }else{
            //验证码不一致
            msg.setStatus(-2);
            msg.setResult("验证码不一致，请检查！");
        }
        String json = JsonUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest req, HttpServletResponse resp){
        Courier courier = UserUtil.getWxCourier(req.getSession());
        Message msg = new Message();
        if (courier!=null){
            //表示是快递员的身份
            msg.setStatus(1);
            msg.setResult(courier.getPhonenumber());
        }else{
            User user = UserUtil.getWxUser(req.getSession());

            //表示是用户的身份
            msg.setStatus(0);
            msg.setResult(user.getPhonenumber());
        }
        String json = JsonUtil.toJSON(msg);
        return json;
    }



    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest req, HttpServletResponse resp){
        //1.销毁session
        req.getSession().invalidate();
        //2.回消息
        Message msg = new Message();
        msg.setStatus(0);
        String json = JsonUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/lazyboard.do")
    public String lazyboard(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Object>> maps = ExpressService.returnUsernameCount();
        Message msg = new Message();
        if (maps==null){
            msg.setStatus(-1);
            msg.setResult("懒人查询失败");
        }else{
            msg.setStatus(0);
            msg.setResult("懒人查询成功！");
            msg.setData(maps);
        }

        return JsonUtil.toJSON(msg);
    }

    @ResponseBody("/wx/getusername.do")
    public String getusername(HttpServletRequest req, HttpServletResponse resp){
        Courier courier = UserUtil.getWxCourier(req.getSession());
        Message msg = new Message();
        if (courier!=null){
            String username = courier.getUsername();
            msg.setData(courier);
        }else{
            User user = UserUtil.getWxUser(req.getSession());
            msg.setData(user);
        }
        return JsonUtil.toJSON(msg);
    }


}
